<?php

namespace Biz\model\wiki;


class BrandArea extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_area';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    public static function admChildSort($sort)
    {
        foreach ($sort as $id => $sort) {
            $where['id'] = $id;
            $data = [
                'sort' => $sort,
                'edittime' => date("Y-m-d H:i:s"),
            ];
            self::update($data, $where)->rowCount();
        }
        return ;
    }

    public static function admDel($id)
    {
        $where = [
            'id' => $id,
        ];
        $data['edittime'] = date("Y-m-d H:i:s");
        $data['area_status'] = self::STATUS_DELETE;
        return self::update($data, $where)->rowCount();
    }

    public static function admGetInfo($id)
    {
        $where = [
            'id' => $id,
            'area_status' => self::STATUS_NORMAL,
        ];
        return self::get('*', $where);
    }

    public static function admSaveInfo($id, $data)
    {
        if (self::admGetInfo($id)) {
            $where = [
                'id' => $id,
                'area_status' => self::STATUS_NORMAL,
            ];
            $data['edittime'] = date("Y-m-d H:i:s");
            return self::update($data, $where)->rowCount();
        } else {
            $data['addtime'] = date("Y-m-d H:i:s");
            self::insert($data);
            return self::id();
        }
    }
    public static function admBrandAreaCount($condition = [])
    {
        $where = [
            'area_status' => self::STATUS_NORMAL,
            'pid' => 0,
            'ORDER' => [
                'sort' => 'ASC'
            ]
        ];
        $condition += $where;
        $count = self::count($condition);
        return  $count;
    }
    public static function admBrandAreaList($condition = [])
    {
        $where = [
            'area_status' => self::STATUS_NORMAL,
            'pid' => 0,
            'ORDER' => [
                'sort' => 'ASC'
            ]
        ];
        $condition += $where;
        $list = self::select('*', $condition);
        $list = self::admFormatList($list);
        return $list;
    }

    public static function admGetBrandAreaById($id)
    {
        $where = [
            'area_status' => self::STATUS_NORMAL,
            'OR' => [
                'id' => $id,
                'pid' => $id,
            ],
            'ORDER' => [
                'sort' => 'ASC'
            ]
        ];
        $list = self::select('*', $where);
        $list = self::admFormatList($list);
        $info = current($list);
        return $info;
    }

    public static function admFormatList($list)
    {
        $response = [];
        $pidArr = empty($list) ? [] : array_column($list, 'id');

        if ($pidArr) {
            $where = [
                'pid' => $pidArr,
                'area_status' => self::STATUS_NORMAL,
                'ORDER' => [
                    'sort' => 'ASC'
                ]
            ];
            $children = self::select('*', $where);
            $childrenMap = [];
            foreach ($children as $v) {
                $childrenMap[$v['pid']][] = $v;
            }

            foreach ($list as $v) {
                if ($v['pid'] == 0) {
                    $v['child'] = $childrenMap[$v['id']] ?? [];
                    $v['child_title'] = empty($v['child']) ? [] : array_column($v['child'], 'title');
                    $response[] = $v;
                }
            }
        }

        return $response;
    }

    public static function getAllByWhere($where = [],$files = '*')
    {
        $data = self::select($files, $where);
        return $data;
    }

    public static function getOneByWhere( $where = [], $files = '*')
    {
        $data = self::get($files, $where);
        return $data;
    }

    public static function addData($data)
    {
        try {
            self::insert($data);
            return self::id();
        } catch (Exception $e) {
            return false;
        }
    }

    public static function updateData($data, $where)
    {
        try {
            self::update($data, $where);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }
}